/**
 *  全局攻击者
 */

let baser = require("./baser");

// 返回baser的内部生命函数
module.exports = function () {
    return baser(
        /**
         * 准备函数，找到攻击者，跟随
         * @param {Creep} creep
         */
        function (creep) {
            let attacker;
            for (let i in Game.creeps) {
                let creep_i = Game.creeps[i];
                if (creep_i.memory.role == 'g_attacker') {
                    attacker = creep_i;
                    creep.memory.attackerid = attacker.id;
                    break;
                }
            }
            if (attacker) {
                if (!creep.pos.isNearTo(attacker.pos)) {
                    creep.moveTo(attacker);
                    return 1;
                }
                return 2;
            }
            return 1;
        },
        /**
         * 工作函数，跟随治疗
         * @param {Creep} creep
         */
        function (creep) {
            let attacker = Game.getObjectById(creep.memory.attackerid);
            if (!attacker) {
                return 1;
            }
            if (creep.heal(attacker) == ERR_NOT_IN_RANGE) {
                creep.moveTo(attacker);
            }
            return 2;
        },
        /**
         * 结束函数，闲逛或其它
         * @param {Creep} creep
         */
        function (creep) {
            return 1;
        });
}